﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using fhq.hcmute.desktop.image;
using fhq.hcmute.core.qrcode.common;
using fhq.hcmute.core.qrcode;
using ModernShopping.AppCode;
using System.IO;
namespace ModernShopping
{
    public partial class Home : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.GetLoginInfo() != null)
                {
                    LoginInfo loginInfo = Request.GetLoginInfo();
                    Session.SetLoginInfo(loginInfo);
                }
            }
            base.OnInit(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            string Keywords = WebUtil.RemoveSign4VietnameseString(TxtKeywords.Text).ToLower();
            Response.GoToSearchPage(Request, TxtKeywords.Text.ToLower());
        }

        protected void btnScanQr_Click(object sender, EventArgs e)
        {
            if (QrUpload.HasFile)
            {
                if (!File.Exists(Server.MapPath(@"~\QR-Search")))
                {
                    Directory.CreateDirectory(Server.MapPath(@"~\QR-Search"));
                }
                string DiaChiHinhAnh = @"~\QR-Search\{0}";
                DiaChiHinhAnh = string.Format(DiaChiHinhAnh, Guid.NewGuid().ToString());
                QrUpload.SaveAs(Server.MapPath(DiaChiHinhAnh));

                BinaryImage img = BufferImageUtil.parse(Server.MapPath(DiaChiHinhAnh));
                QRCodeReader reader = new QRCodeReader(img);
                QRCode qrCode = reader.decode();
                string[] data = qrCode.getContents().Split(';');
                int ProductCode = int.Parse(data[0]);
                Response.GoToProductInfoPage(Request, ProductCode);
            }
        }
    }
}